# -*- coding: utf-8 -*-
"""
Created on Wed Aug 11 17:05:50 2021

@author: xiong
"""
import arcpy.mapping as mp
import sys
import arcpy
reload(sys)
sys.setdefaultencoding('utf8') 
import csv
import time
'粘贴到gis里边运行，出当前内容列表里边所有含有温度带关键词的点shp的csv，对应的图斑跟点都要在'


def writelog(str0):
    localtime = time.asctime( time.localtime(time.time()) )
    f = open(workspace +'\\log.txt','a')
    f.write(localtime +':' + str(str0)+'\n' )
    f.close()

workspace = r'E:\tutu\csv' #放csv的地方

writelog('start')
mxd = mp.MapDocument('CURRENT')
layers = mp.ListLayers(mxd) 
rtn = 0
l = []
for lyr in layers:
    l.append(lyr.name)
header = ['FID', 'GRID_CODE', 'ID', 'tb', 'E_sum', 'p_pe', 'p1_pe1', 'p2_pe2', 'p3_pe3', 'p4_pe4', 'p5_pe5', 'p6_pe6', 'p7_pe7', 'p8_pe8', 'p9_pe9', 'p10_pe10', 'p11_pe11', 'p12_pe12', 'X', 'Y', 'p','pe','dem','t_prot_yea', 'tbclass', 'belong_tb','all_count']
for lyr in layers:
    count1 = 0
    if '图斑'  in lyr.name:
        continue
    kk = lyr.name + '_图斑'
    writelog(kk)
    if kk not in l:
        writelog(lyr.name+'图斑缺失')
        continue
    
    for i in ['交错带','高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带', '热带']:
        if i in lyr.name:
            break
        count1 += 1

    if count1 == 8 :
        continue
    tb0str = ''
    suic = 0
    if count1 == 0 :
        suic = 1
        tb0str = []
        for i in range(6):
            if ['高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带'][i] in lyr.name:
                tb0str.append(str(i+1))
        ee = lyr.name.replace('亚热带','')
        if '热带' in ee:
            tb0str.append('7')
        tb0str = '.'.join(i for i in tb0str)
    writelog(lyr.name+str(count1) + tb0str)
    belongtbList =  [[] for i in range(8)] 
    tb0dic = {}
    tb0List = []
    
    with arcpy.da.UpdateCursor(kk,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
        for row in cursor:
            str0 = str(int(row[-3])) + '.' + str(int(row[-2])) + '.' + str(int(row[-1])) 
            if str0 not in tb0List:
                tb0dic[str0] = [row]
                tb0List.append(str0)
            else:
                tb0dic[str0] .append(row)
    
    with arcpy.da.UpdateCursor(kk,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
        for row in cursor:
            cursor.deleteRow()
            writelog('图斑已清空')
    datadic = {}   
    writelog(tb0List)  
    for sr in tb0List:
        with arcpy.da.InsertCursor(kk,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
            for row in tb0dic[sr]:
                 cursor.insertRow(row)

        datadic[sr] = []       
        arcpy.SelectLayerByAttribute_management(lyr.name,"NEW_SELECTION",'"tb"> 0')
        arcpy.SelectLayerByLocation_management(lyr.name,"COMPLETELY_WITHIN",kk)
        with arcpy.da.UpdateCursor(lyr.name,header)as coursor:
            for row in coursor:
                datadic[sr].append(row)
        with arcpy.da.UpdateCursor(kk,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
            for row in cursor:
                cursor.deleteRow()
                writelog('图斑已清空')
    for sr in tb0List:
        with arcpy.da.InsertCursor(kk,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
            for row in tb0dic[sr]:
                 cursor.insertRow(row)
    for i in tb0List:
        with open(workspace + '\\' + str(count1) + '.' + i+('_'+tb0str)*suic +'.csv' , "wb") as f:
            writelog(str(suic) + tb0str)
            writer = csv.writer(f)
            writer.writerows([header])
            for g in datadic[i]:
                writer.writerows([g])
            f.close()
       
    writelog(lyr.name+'结束')
    
